package com.jy.adscm.utils;

import com.jy.adscm.exception.AdscmSystemException;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.configurationprocessor.json.JSONException;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yyy
 * @ClassName AdscmDateUtils
 * @Description 时间工具类
 * @date 2018/8/13 17:24
 */
public class AdscmDateUtils {

    /**
     * 格式化秒为： xx时xx分xx秒
     * @param seconds
     * @return
     */
    public static String getInputSecondsOuptHourMin(long seconds) {
        if (seconds < 60) {
            return String.valueOf(seconds) + "秒";
        } else if(seconds >= 3600){
            if (seconds == 3600){
                return "1 时 0 分 0 秒";
            }else {
                int hours = (int) Math.floor(seconds / 3600);
                seconds = seconds % 3600;
                if (seconds == 0){
                    return String.valueOf(hours) + "时 0 分 0 秒";
                }
                Long min = seconds / 60;
                Long s = seconds % 60;
                if (s == 0) {
                    return String.valueOf(hours) + " 时 " + String.valueOf(min) + "分";
                }
                return String.valueOf(hours) + " 时 " + String.valueOf(min) + " 分 " + String.valueOf(s) + " 秒";
            }

        }else {
            Long min = seconds / 60;
            Long s = seconds % 60;
            if (s == 0) {
                return String.valueOf(min) + "分";
            }
            return String.valueOf(min) + "分" + String.valueOf(s) + "秒";
        }
    }


    /**
     * 得到指定日期所在的周是从那天开始 到哪天结束的，是多少周
     * @param time
     * @return
     * @throws ParseException
     */
    public static Map<String, Object> getWeekByDate(Date time) throws ParseException {
        Map<String, Object> map=new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        String imptimeBegin = sdf.format(cal.getTime());
        System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        System.out.println("所在周星期日的日期：" + imptimeEnd);
//        if (time.getTime()>=sdf.parse(imptimeBegin).getTime()&&cal.getTime().getTime()<=sdf.parse(imptimeEnd).getTime()) {
        map.put("benginDate", imptimeBegin);
        map.put("endDate", imptimeEnd);
        map.put("whereWeek", cal.get(Calendar.WEEK_OF_YEAR));
//        }
        return map;
    }
    public static int getWeek(Date time,Date time1) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }

        int a=cal.get(Calendar.WEEK_OF_YEAR);
        cal.setTime(time1);
        int b=cal.get(Calendar.WEEK_OF_YEAR);


        return a-b;
    }

    /**
     * 判断指定日期是哪一周
     * @param time
     * @return
     * @throws ParseException
     */
    public static int getWeek(Date time) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }

        int a=cal.get(Calendar.WEEK_OF_YEAR);

        return a;
    }

    /**
     * 获取指定日期的前或者后num年/月/天，根据类型
     * @param type : 类型：type = Calendar.YEAR、Calendar.MONTH、Calendar.DATE
     * @return : yyyy-MM-dd
     */
    public static String getThatDateStr(Date date, int num, int type){
        return AdscmDateUtils.getNowOnlyDateFomat1(getThatDate(date,num,type));
    }
    /**
     * 获取指定日期的前或者后num年/月/天，根据类型
     * @param type : 类型：type = Calendar.YEAR、Calendar.MONTH、Calendar.DATE
     * @return : Date
     */
    public static Date getThatDate(Date date, int num, int type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, num);
        date = calendar.getTime();
        return date;
    }
    /**
     * 获取指定日期当天的最后一秒钟
     * @return : Date
     */
    public static Date getThatDateTime(Date date){
        Date newDate = null;
        try{
            String d = new SimpleDateFormat("yyyyMMdd").format(date) + " 23:59:59";
            newDate = new SimpleDateFormat("yyyyMMdd HH:mm:ss").parse(d);
        }catch (Exception e){}
        return newDate;
    }

    /**
     * 将制定格式的字符串转换成日期，字符串格式：yyyy-MM-dd
     * @return
     */
    public static Date parseDate(String dateStr) throws AdscmSystemException{

        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (ParseException e) {
            throw new AdscmSystemException(e.getMessage());
        }
        return date;
    }
    /**
     * 将制定格式的字符串转换成日期，字符串格式：yyyyMMdd
     * @return
     */
    public static Date parseDate2(String dateStr) throws AdscmSystemException{

        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(dateStr);
        } catch (ParseException e) {
            throw new AdscmSystemException(e.getMessage());
        }
        return date;
    }
    /**
     * 格式化指定日期，格式：yyyy-MM-dd
     * @return
     */
    public static String getNowOnlyDateFomat1(Date date){
        String dfs = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return dfs;
    }
    /**
     * 格式化指定日期，格式：yyyyMMdd
     * @return
     */
    public static String getNowOnlyDateFomat2(Date date){
        String dfs = new SimpleDateFormat("yyyyMMdd").format(date);
        return dfs;
    }

    /**
     * 格式化指定日期，格式：yyMMdd
     * @return
     */
    public static String getNowOnlyDateFomat3(Date date){
        String dfs = new SimpleDateFormat("yyMMdd").format(date);
        return dfs;
    }
    /**
     * 格式化指定日期，格式：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getDateFomat(Date date){
        String dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        return dfs;
    }

    /**
     * @param date
     * @return格式：yyyy-MM-dd
     */
    public static String getDateFomats(Date date){
        String dfs = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return dfs;
    }
    public static Date getDateFomat(String date){
        Date dfs  = null;
        try {
            dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dfs;
    }

    /**
     * 获取两个日期之间的间隔的每一天
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getBetweenDate(Date startDate,Date endDate){

        List<String> list = new ArrayList<>();
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String sdStr = format.format(startDate);
            String edStr = format.format(endDate);

            Date sdDate = format.parse(sdStr);
            Date edDate = format.parse(edStr);

            while (edDate.getTime() > sdDate.getTime()){
                list.add(AdscmDateUtils.getNowOnlyDateFomat2(sdDate));
                sdDate = AdscmDateUtils.getThatDate(sdDate,1,Calendar.DATE);
            }
            list.add(AdscmDateUtils.getNowOnlyDateFomat2(edDate));
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }
    /**
     * 传入一个时间，得到当前月的第一天 比如2018-06-01
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfTheMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH,1);//1:本月第一天
        return   getFormateDate(calendar.getTime());
    }

    /**
     * 传入一个时间，得到当前月的最后一天 比如2018-06-30
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfTheMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String dateFomats = getDateFomats(calendar.getTime())+" 23:59:59";
        return  getDateFomat(dateFomats);
    }
    /**
     * 传入一个时间，得到年
     *
     * @param date
     * @return
     */
    public static int getTheYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
    /**
     * 传入一个时间，得到月
     *
     * @param date
     * @return
     */
    public static int getTheMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     *根据一段日期算出中间是否有多少个周末
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public  static  int getSunday(Date startDate,Date endDate) {
        int temp= 0;
        try {
            temp = 0;
            List<String> betweenDate = getBetweenDate(startDate, endDate);
            for (String date : betweenDate) {
                Date tempdate = parseDate2(date);
                boolean isSunDay = getIsSunDay(tempdate);
                if (isSunDay){
                    temp++;
                }
            }
        } catch (AdscmSystemException e) {
            e.printStackTrace();
        }

        return temp;
    }

    /**
     * 判断是否是周末
     * @param date
     * @return
     */
    public static boolean getIsSunDay( Date date){
        boolean result=false;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        if (w==0){
            return  true;
        }
        return result;
    }

    /**
     * @param date
     * @return
     */
    public static Date getFormateDate( Date date){
        Date parse = null;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = dateFormat.format(date);
            parse = dateFormat.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return parse;
    }

    /**
     * 计算时间差
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return  hour + "小时" + min + "分钟";
    }
    public  static  boolean isSameDay(Date date1,Date date2){
        String dateFomat1 = getNowOnlyDateFomat3(date1);
        String dateFomat2 = getNowOnlyDateFomat3(date2);
        if (dateFomat1.equals(dateFomat2)){
            return true;
        }
        return false;
    }
    /**
     * 根据时间 进行加法运算
     * @param time
     * @return
     */
    public static Date dateCalculate(Date date,Integer time){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+time);//让日期加1
        Date time1 = calendar.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        return time1;
    }

    /**
     * 根据时间 进行减法运算
     * @param time
     * @return
     */
    public static   Date dateCount(Date date,Integer time){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)-time);//让日期加1
        Date time1 = calendar.getTime();
        return time1;
    }


    public static String getDateFomatHour(Date date){
        String dfs = new SimpleDateFormat("HH:mm:ss").format(date);
        return dfs;
    }

    static String []  fdjr={
                      "2019-09-13",
                       "2019-09-14",
                       "2019-09-15",
                       "2019-10-01",
                       "2019-10-02",
                       "2019-10-03",
                       "2019-10-04",
                       "2019-10-05",
                       "2019-10-06",
                       "2019-10-07"
                      };
    static  String [] fdgzr={
            "2019-09-29",
            "2019-10-12",
            };
    static  String temp="";
    public static String isWorkDate(String date){
        try {
            Date parseDate = parseDate(date);
            Calendar calendar =Calendar.getInstance();
            calendar.setTime(parseDate);
            //是否是法定工作日
            for (String s : fdgzr) {
                if (s.equals(date)){
                    System.out.println("法定工作日"+date);
                    temp= date;
                    return temp;
                }
            }


            if (calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY ||isFdjjr(date)){
                System.out.println("周末或者节假日"+date);
                String nextDay = AdscmDateUtils.getThatDateStr(AdscmDateUtils.parseDate(date), -1, Calendar.DATE);
                isWorkDate(nextDay);
            }else {
                System.out.println("工作日"+date);
                temp=date;
            }


        } catch (AdscmSystemException e) {
            e.printStackTrace();
        }

        return temp;
    }
    public static  boolean isFdjjr(String date){
        //先判断是否不是法定假日,如果是法定假日就加一
        for (String s : fdjr) {
            if (s.equals(date)){
                return true;

            }
        }
     return false;
    }

    /**
     * 传入一个时间获取到这个时间这周的周一
     * @param date
     * @return
     */
    public  static  Date getMonday( Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date time = calendar.getTime();
        Date formateDate = getFormateDate(time);
        return formateDate;
    }
    public  static  Date getSunDay(Date date){
        Date calculate = dateCalculate(date, 7);
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(calculate);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        Date time = calendar.getTime();
        Date formateDate = getFormateDate(time);
        return formateDate;
    }

    public  static  boolean isMonday(Date date ){
        Boolean result=false;
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int weekday=calendar.get(Calendar.DAY_OF_WEEK);
        if (weekday==2){
            result=true;
        }
        return result;
    }
    public  static boolean isMonthStart(Date date){
        Boolean result=false;
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int weekday=calendar.get(Calendar.DAY_OF_MONTH);
        if (weekday==1||weekday==2 ||weekday==3){
            result=true;
        }
        return result;
    }
    public static void main(String[] args) throws Exception{
//        Map<String, Object> map = AdscmDateUtils.getWeekByDate(new Date());
//        String str = "2019-"+ 12 +"-01 08:29:59";
//        Date d = getDateFomat(str);
//        System.out.println(getDateFomat(d));
//        for (int i=0;i<263;i++){
//            d= dateCalculate(d, 7);
//            System.out.println(getDateFomat(d));
//        }
        System.out.println(getDateFomat(AdscmDateUtils.getLastDayOfTheMonth(new Date())));;

    }



}
